<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Brand;
use App\Post;
use Auth;
use DB;
use App\sellcars;
use Image;
use Redirect;
use App\Carmodel;
use App\Tocar;
use App\Car;
use Input;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *后台首页控制器
     * @return \Illuminate\Http\Response
     */
    
    //检测登陆中间件
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];

    public function index(Request $req)
    {
        
            return view('admin/index');

    }

// if (Auth::attempt(array('name' => $req['name'],'password'=>$req['password']), true))
// {

// }

    //输出admin信息及main页面展示
    public function adm(){                  

        $users = Auth::user();
        return view('admin/adm',['users'=>$users]);
    }
    //输出brand库中的信息供页面选取
    //输出addcar页面
    public function addcar(Request $req){
       if ($req->all()!= null) {
        // dd($req->all());die;
        $rand = md5(time());
            $images=$req->file('p_pics'); //使用laravel 自带的request类来获取一下文件.
            $filedir="admin/upload/"; //定义图片上传路径
            $smalldir="admin/small/";//定义小图上传路径
            $newImagesName='car'.$rand.'.jpg'; //重新命名上传文件名字 
            $images->move($filedir,$newImagesName); //使用move 方法移动文件
            $path = $filedir.$newImagesName;
            //打开一个大图
            $img = Image::make("$path");

            // // resize image instance
            $img->resize(320, 240);
            //
            // $img->insert('admin/small/'.$newImagesName);
            //插入到本地
            $smallpath = $smalldir.$newImagesName;
            $img->save($smallpath);

            $cars = new car();
            $cars->p_brand = $req->p_brand;
            $cars->p_subbrand = $req->p_subbrand;
            $cars->p_subsubbrand = $req->p_subsubbrand;
            $cars->p_name = $req->p_name;
            $cars->p_price = $req->p_price;
            $cars->p_color = $req->p_color;
            $cars->p_year = $req->p_year;
            $cars->p_model = $req->p_model;
            $cars->p_gas = $req->p_gas;
            $cars->p_month = $req->p_month;
            $cars->p_transmission = $req->p_transmission;
            $cars->p_kilometre = $req->p_kilometre;
            $cars->p_country = $req->p_country;
            $cars->p_emission = $req->p_emission;
            $cars->p_details = $req->p_details;
            $cars->p_username = $req->p_username;
            $cars->p_tel = $req->p_tel;
            $cars->ishot = $req->ishot;
            $cars->isrecom = $req->isrecom;
            $cars->isshow = $req->isshow;
            $cars->p_pics = $path;
            $cars->p_mainpic = $smallpath;


            //同时将数据插入sellcars表中
            $sellcars = new sellcars();
            $sellcars->p_brand = $req->p_brand;
            $sellcars->p_subbrand = $req->p_subbrand;
            $sellcars->p_subsubbrand = $req->p_subsubbrand;
            $sellcars->p_name = $req->p_name;
            $sellcars->p_price = $req->p_price;
            $sellcars->p_color = $req->p_color;
            $sellcars->p_year = $req->p_year;
            $sellcars->p_model = $req->p_model;
            $sellcars->p_gas = $req->p_gas;
            $sellcars->p_month = $req->p_month;
            $sellcars->p_transmission = $req->p_transmission;
            $sellcars->p_kilometre = $req->p_kilometre;
            $sellcars->p_country = $req->p_country;
            // $sellcars->p_emission = $req->p_emission;
            $sellcars->p_details = $req->p_details;
            // $sellcars->p_username = $req->p_username;
            // $sellcars->p_tel = $req->p_tel;
            // $sellcars->ishot = $req->ishot;
            // $sellcars->isrecom = $req->isrecom;
            // $sellcars->isshow = $req->isshow;
            $sellcars->p_pics = $path;
            $sellcars->p_mainpic = $smallpath;

            if ($cars->save()&$sellcars->save()) {



                 return Redirect::to('admin/addcar'); 

            }else{

                 return Redirect::to('admin/addcar');
            }
       }else{
        
        $carmodel = new Carmodel();
        $model = $carmodel->get();
        $cars = DB::table('brands')->where('b_parent', '-1')->get();
         return view('admin/add_cars',['cars'=>$cars,'model'=>$model]);
    }





}
 //Ajax父级栏目请求地址

    public function getAttributeByType(){
        $bid = $_GET['b_id'];
        $carss = DB::table('brands')->where('b_id',$bid)->first();
        $b_id = $carss->b_id;
        $cha = DB::table('brands')->where('b_parent',$b_id)->get();
        echo json_encode($cha);
    }
    //Ajax子级栏目请求地址
     public function songetAttributeByType(){
        // return 123;
        $bid = $_GET['b_id'];
        $carss = DB::table('brands')->where('b_id',$bid)->first();
        $b_id = $carss->b_id;
        $cha = DB::table('brands')->where('b_parent',$b_id)->get();
        echo json_encode($cha);
    }


    //HTML main页。

    public function clist(){
        return view('admin.cars_list');
    }


    //后台汽车品牌列表

      public function blist(){
        $brandlist = DB::table('brands')->paginate(14);
        return view('admin.brand_list',['brandlist'=>$brandlist]);
    }

    //后台增加品牌页面
         public function addbr(Request $req){
            // return view('admin/add_brand');
            $bid = $req->bid;
            // dd($bid);
            $msg = Brand::find($bid);
            // dd($msg);
            return view('admin/add_brand',['msg'=>$msg]);
            // return view('admin/add_brand');
    }

    //添加品牌
    public function postAddbr(Request $req , $bid){
            $row = new Brand();
           /* $row['b_id'] = $req->b_id;
            $row['b_name'] = $_POST['b_name'];
            $row['b_keyword'] = $_POST['b_keyword'];*/
            $fname = date('Ymd').mt_rand(1000,9999).'.jpg';
            $fpath = 'admin/img/';
            $pic = $req->file('pic')->move($fpath,$fname);
            
                    // 'b_id'=>$req->b_id,
            $row->b_type = $_POST['b_type'];
            $row->b_name = $_POST['b_name'];
            $row->b_keyword = $_POST['b_keyword'];
            $row->pic = $fpath.$fname;
            $row->save();

            $msg = Brand::find($bid);
            // $a =['b'];
             // var_dump($msg);exit;
            return view('admin/add_brand',['msg'=>$msg]);

            // $blist = DB::table('brands')->where('b_id',$bid)->first();

    }

    //删除品牌
    public function delBrand(Request $req , $bid){
        $rs = DB::table('brands')->where('b_id',$bid)->delete();
        return $rs ?  "OK" : "del fail";
    }

    //编辑品牌
    public function editBrand(Request $req ,$bid){
            /*if (empty($_POST)) {
                $bl = DB::table('brands')->where('b_id',$bid)->first();
                return view('admin/edit_brand',['bl'=>$bl]);
                
            }else{*/
                /*Brand::->where('b_id',$bid)->first();
                $fname = date('Ymd').mt_rand(1000,9999).'.jpg';
                $fpath = 'admin/img/';
                $pic = $req->file('pic')->move($fpath,$fname);

                DB::table('msgs')->where('b_id',$bid)->update([
                        'b_type' => $_POST['b_type'],
                        'b_name'=>$_POST['b_name'],
                        'b_keyword'=>$_POST['b_keyword'],
                        'pic'=>$fpath.$fname,
                        ]);*/
                
            // }
            if (empty($_POST)) {
                $bra = Brand::find($bid);
                return view('admin/edit_brand',['bra'=>$bra]);
            }else{
                $bra = Brand::find($bid);
                $bra->b_type = $_POST['b_type'];
                $bra->b_name = $_POST['b_name'];
                $bra->b_keyword = $_POST['b_keyword'];
                return $bra->save() ? "OK" : "fail";
            }
            
            
    }
    //车型list页面
    public function modellist(Request $req){
        if (!empty($_POST)) {
            // $req->all();
        }else{
        $model = Carmodel::get();
        // dd($model);
        return view('admin/modellist',['model'=>$model]);
    }
}
    //车型添加页面
    public function addmodel(Request $req){
        if ($req->all()!=null) {
            $file = $req->file('s_pic');
            $smalldir="admin/small/";//定义小图上传路径
            $rand = md5(time());
            $newImagesName='car'.$rand.'.jpg'; //重新命名上传文件名字 
            $img = Image::make("$file");
            // // resize image instance
            $img->resize(320, 240);
            // $img->insert('admin/small/'.$newImagesName);
            //插入到本地
            $smallpath = $smalldir.$newImagesName;
            $img->save($smallpath);
            //插入数据库
            $carmodel = new Carmodel();
            $carmodel->s_name = $req->s_name;
            $carmodel->s_pic = $smallpath;
            if ($carmodel->save()) {
                 return Redirect::to('admin/modellist'); 
            }else
                 return Redirect::to('admin/addmodel'); 

        }else{

        return view ('admin/addmodel');
        }
    }
    //车型修改
    public function modelupd(Request $req){
         $sid = $req->sid;
        if (!empty($_POST)) {
        $file = $req->file('s_pic');
        $smalldir="admin/small/";//定义小图上传路径
        $rand = md5(time());
        $newImagesName='car'.$rand.'.jpg'; //重新命名上传文件名字 
        $img = Image::make("$file");
        // // resize image instance
        $img->resize(320, 240);
        // $img->insert('admin/small/'.$newImagesName);
        //插入到本地
        $smallpath = $smalldir.$newImagesName;
        $img->save($smallpath);
        $carmodel = new Carmodel();
        $rs = $carmodel->where('sid',$sid)->update(
        ['s_name'=>$req->s_name,
        's_pic'=>$smallpath,
        ]);
        if ($rs) {
        return Redirect::to('admin/modellist'); 
        }else
        return Redirect::to('admin/addmodel'); 
        }else{
        $sid = $req->sid;
        $carmodel = new Carmodel();
        $model = $carmodel->find($sid);
        return view('admin/modelupd',['model'=>$model]);
    }
}
    //车型删除
    protected function modeldel(Request $req){
         $sid = $req->sid;
          $rs = DB::table('carmodels')->where('sid',$sid)->delete();
    if ($rs) {
        return Redirect::to('admin/modellist'); 
        }else
        return Redirect::to('admin/addmodel'); 

    }

    public function ping(){
        return view('admin/ping');
    }

    public function buycar(){
        $brands=DB::table('tocars')->join('brands', 
            'brands.b_id', '=', 'tocars.p_brand'
            )->get();
        // $rs = DB::table('brands')->wherein('b_id',array(5,2,3))->get();
        return view('admin/buycar',['brands'=>$brands]);
        }

    public function yuyue(){

        return view('admin/yuyue');

        }

    public function xun(){

        return view('admin/xun');

        }

}
